package com.hhkj.study.admin.dict.service.extend.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.dict.common.LevelCodeEnum;
import com.hhkj.study.admin.dict.entity.DictMultiLevel;
import com.hhkj.study.admin.dict.model.podo.DictMultiLevelDo;
import com.hhkj.study.admin.dict.model.podo.DictMultiLevelPo;
import com.hhkj.study.admin.dict.service.base.DictMultiLevelService;
import com.hhkj.study.admin.dict.service.extend.DictMultiLevelExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author SERVER
 * @since 2024-09-06
 */
@Service
public class DictMultiLevelExtendServiceImpl extends BaseExtendServiceImpl<DictMultiLevel, DictMultiLevelDo, DictMultiLevelPo, DictMultiLevelService> implements DictMultiLevelExtendService {

    @Override
    public Map<Long, String> getDictMap(List<String> typeCodeList) {
        List<DictMultiLevel> dictList = this.getService().getDictListEnable(typeCodeList);
        Map<Long, String> dictMap =  Maps.newHashMap();
        if (CollectionUtil.isEmpty(dictList)) {
            return dictMap;
        }
        return dictList.stream().filter(dic -> 0L != dic.getParentId()).collect(Collectors.toMap(DictMultiLevel::getId, DictMultiLevel::getName));
    }

    @Override
    public Map<Long, String> getDictParentNameMap(List<String> typeCodeList) {
        List<DictMultiLevel> dictList = this.getService().getDictListEnable(typeCodeList);
        if (CollectionUtil.isEmpty(dictList)) {
            return Maps.newHashMap();
        }
        return dictList.stream().filter(dic -> 0L != dic.getParentId()).collect(Collectors.toMap(
                DictMultiLevel::getId,
                dict -> {
                    // 查找父元素的名称
                    String parentName = dictList.stream()
                            .filter(d -> d.getId().equals(dict.getParentId()))
                            .findFirst()
                            .map(DictMultiLevel::getName)
                            .orElse(""); // 如果没有找到父元素，或者parentId为null，则使用空字符串
                    // 返回父名称-子名称
                    return StrUtil.isBlank(parentName) || LevelCodeEnum.SECOND.getCode().equals(dict.getLevelCode()) ? dict.getName() : parentName + "-" + dict.getName();
                },
                (existingValue, newValue) -> existingValue
        ));
    }

    @Override
    public List<Long> getAllCourseIdByParentId(Long parentId) {
        LambdaQueryWrapper<DictMultiLevel> dictMultiLevelLambdaQuery = new LambdaQueryWrapper<>();
        dictMultiLevelLambdaQuery.eq(DictMultiLevel::getParentId, parentId).eq(DictMultiLevel::getLevelCode, LevelCodeEnum.THIRD.getCode());
        List<DictMultiLevel> dictMultiLevelList = this.getService().list(dictMultiLevelLambdaQuery);
        return dictMultiLevelList.stream().map(DictMultiLevel::getId).collect(Collectors.toList());
    }
}
